package leetcode.D500.T413;

class Solution {
    /* 暴力法，O(n2)
    public int numberOfArithmeticSlices(int[] nums) {
        if(nums.length < 3) return 0;
        int count = 0;
        for(int i=0; i<nums.length-2; ++i) {
            int diff = nums[i+1] - nums[i];
            for(int j=i+2; j<nums.length; ++j) {
                if(nums[j] - nums[j-1] != diff) {
                    break;
                } else {
                    count++;
                }
            }
        }
        return count;
    }*/
    public int numberOfArithmeticSlices(int[] nums) {
        if(nums.length < 3) return 0;
        int dp = 0, sum = 0;
        for(int i=2; i<nums.length; ++i) {
            if(nums[i]-nums[i-1] == nums[i-1]-nums[i-2]) {
                dp = dp + 1;
            } else dp = 0;
            sum += dp;
        }
        return sum;
    }
}